﻿using VIC.User.Models;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Users.Services;
using Orchard.Security;
using Orchard.Localization;
using VIC.User.ViewModels;
using Orchard.Core.Common.Models;
using System;

namespace VIC.User.Drivers
{
    public class VIC_UserDriver : ContentPartDriver<VIC_UserPart>
    {
        int MinPasswordLength
        {
            get
            {
                return _membershipService.GetSettings().MinRequiredPasswordLength;
            }
        }
        private readonly IUserService _userService;
        private readonly IMembershipService _membershipService;
        public Localizer T { get; set; }
        public VIC_UserDriver(
            IMembershipService membershipService,
            IUserService userService)
        {
            _membershipService = membershipService;
            _userService = userService;
            T = NullLocalizer.Instance;
        }
        protected override DriverResult Display(
            VIC_UserPart part, string displayType, dynamic shapeHelper)
        {

            return ContentShape("Parts_VIC_User", () => shapeHelper.Parts_VIC_User(
                FirstName: part.FirstName,
                LastName: part.LastName,
                Gender: part.Gender,
                DateOfBirth: part.DateOfBirth,
                Email: part.Email,
                Address: part.Address,
                Phonenumber: part.Phonenumber,
                SummaryCare: part.SummaryCare,
                SummarySelf: part.SummarySelf,
                Website: part.Website));
        }

        //GET
        protected override DriverResult Editor(
            VIC_UserPart part, dynamic shapeHelper)
        {
            if (part.UserPart_Id == 0)
            {
                return ContentShape("Parts_VIC_User_Edit",
                    () => shapeHelper.EditorTemplate(
                        TemplateName: "Parts/VIC_User_New",
                        Model: new VIC_UserNewVM(),
                        Prefix: Prefix));
            }
            else
            {
                return ContentShape("Parts_VIC_User_Edit",
                    () => shapeHelper.EditorTemplate(
                        TemplateName: "Parts/VIC_User_Edit",
                        Model: part,
                        Prefix: Prefix));
            }
        }
        //POST
        protected override DriverResult Editor(
            VIC_UserPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            if (part.UserPart_Id == 0)
            {
                VIC_UserNewVM Model = new VIC_UserNewVM();
                
                if (updater.TryUpdateModel(Model, Prefix, null, null))
                {
                    if (!_userService.VerifyUserUnicity(Model.Email, Model.Email))
                    {
                        updater.AddModelError("VerifyUserUnicity", T("User with that username and/or email already exists."));
                        goto Error;
                    }

                    if (Model.Password == null || Model.Password.Length < MinPasswordLength)
                    {

                        updater.AddModelError("VerifyUserUnicity", T("You must specify a password of {0} or more characters.", MinPasswordLength));
                        goto Error;
                    }

                    if (!String.Equals(Model.Password, Model.ConfirmPassword, StringComparison.Ordinal))
                    {
                        updater.AddModelError("_FORM", T("The new password and confirmation password do not match."));
                        goto Error;
                    }

                    var user = _membershipService.CreateUser(new CreateUserParams(Model.Email, Model.Password, Model.Email, null, null, true));
                    if (user == null)
                    {
                        updater.AddModelError("VerifyUserUnicity", T("Could not create user {0}. The authentication provider returned an error", Model.Email));
                        goto Error;
                    }

                    else
                    {
                        part.Email = Model.Email;
                        part.Gender = Model.Gender;
                        part.DateOfBirth = Model.DateOfBirth;
                        part.Phonenumber = Model.Phonenumber;
                        part.Address = Model.Address;
                        part.FirstName = Model.FirstName;
                        part.LastName = Model.LastName;
                        part.SummaryCare = Model.SummaryCare;
                        part.SummarySelf = Model.SummarySelf;
                        part.Website = Model.Website;
                        part.UserPart_Id = user.Id;
                        part.ContentItem.As<CommonPart>().Owner = user;
                    }
                }
            }
            else
            {
                VIC_UserEditVM Model = new VIC_UserEditVM();
                if (updater.TryUpdateModel(Model, Prefix, null, null))
                {
                    part.Email = Model.Email;
                    part.Gender = Model.Gender;
                    part.DateOfBirth = Model.DateOfBirth;
                    part.Phonenumber = Model.Phonenumber;
                    part.Address = Model.Address;
                    part.FirstName = Model.FirstName;
                    part.LastName = Model.LastName;
                    part.SummaryCare = Model.SummaryCare;
                    part.SummarySelf = Model.SummarySelf;
                    part.Website = Model.Website;
                }
            }
        Error:
            return Editor(part, shapeHelper);

        }
    }
}